# -*- coding:utf8 -*-
# 崔烁豪
# 时间：2021-05-17 15:30
import inline as inline
import paddle
from paddle.vision.transforms import Compose, Normalize
from paddle.vision.datasets import MNIST
import paddle.nn as nn

# 数据预处理，这里用到了随机调整亮度、对比度和饱和度
transform = Normalize(mean=[127.5], std=[127.5], data_format='CHW')

# 数据加载，在训练集上应用数据预处理的操作
train_dataset = MNIST(mode='train', transform=transform)
test_dataset = MNIST(mode='test', transform=transform)

# 模型组网
mnist = nn.Sequential(
    nn.Flatten(),
    nn.Linear(784, 512),
    nn.ReLU(),
    nn.Dropout(0.2),
    nn.Linear(512, 10)
)

# 模型封装，用Model类封装
model = paddle.Model(mnist)

# 模型配置：为模型训练做准备，设置优化器，损失函数和精度计算方式
model.prepare(optimizer=paddle.optimizer.Adam(parameters=model.parameters()),
              loss=nn.CrossEntropyLoss(),
              metrics=paddle.metric.Accuracy())

# 模型训练，
model.fit(train_dataset,
          epochs=5,
          batch_size=64,
          verbose=1)

# 模型评估，
model.evaluate(test_dataset, verbose=1)

print("飞桨框架CV领域内置数据集：" + str(paddle.vision.datasets.__all__))
